import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;


// 服务端
public class ServerSocketDemo01 {
    public static void main(String[] args) throws IOException {
        // 创建一个套接字对象，并监听9000端口
        ServerSocket serverSocket = new ServerSocket(9001);
        // 等待客户端连接
        System.out.println("等待客户端连接");
        Socket accept = serverSocket.accept();
        System.out.println("客户端连接成功" + accept.getInetAddress().getHostName());
        //获取输入流对象
        InputStream inputStream = accept.getInputStream();
        byte[] bytes = new byte[1024];
        System.out.println(inputStream.read(bytes));
        int len;
        while ((len = inputStream.read(bytes)) != -1) {
            System.out.println(new String(bytes, 0, len));
        }
        // 关闭socket对象
        if (!accept.isClosed()) {
            accept.close();
        }

    }
}
